package cn.kuaipan.android.openapi.tasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import cn.kuaipan.android.openapi.KuaipanAPI;
import cn.kuaipan.android.openapi.RequestBase;
import cn.kuaipan.android.openapi.ResultBase;
import cn.kuaipan.android.sdk.exception.KscException;
import cn.kuaipan.android.sdk.exception.KscRuntimeException;
import cn.kuaipan.android.sdk.model.KuaipanFile;

public class MetaDataTask extends MyAsyncTask<RequestBase, Void, ResultBase> {

        private ArrayList<KuaipanFile> mFileDataList;
		private final String TAG = MetaDataTask.class.getSimpleName();

		@Override
        protected void onPostExecute(ResultBase result) {
            

            //if (TextUtils.isEmpty(result)) {
            if(result == null){
                //showToast("Get Account Info is Failed");
                return;
            }

//            ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
//            for (int i = 0; i < mFileDataList.size(); i++) {
//                HashMap<String, Object> map = new HashMap<String, Object>();
//                map.put("FileItem", mFileDataList.get(i).path);
//                listItem.add(map);
//            }

           
        }

        @Override
        protected ResultBase doInBackground(RequestBase params) {
            try {
            	RequestBase reqeust = params;
                String filepath = reqeust.getRemotePath();
                KuaipanAPI mApi = reqeust.getApi();
                KuaipanFile file = mApi.metadata(filepath);
                mFileDataList.clear();

                getAllFiles(mFileDataList, file);
                Log.v(TAG , "metadata:" + file.toString());
                ResultBase result = new ResultBase();
                result.setFilePath( file.toString());
                return result;
            } catch (KscRuntimeException e) {
                e.printStackTrace();
                Log.v(TAG,
                        "KscRuntimeException--Failed!!!" + e.getErrorCode());
            } catch (KscException e) {
                e.printStackTrace();
                Log.v(TAG, "KscException--Failed!!!" + e.getErrorCode());
            } catch (InterruptedException e) {
                e.printStackTrace();
                Log.v(TAG, "InterruptedException--Failed!!!");
            }
            return null;
        }
    

	public void getAllFiles(ArrayList<KuaipanFile> list, KuaipanFile file) {
	    list.add(file);
	    if (file.isDirectory()) {
	        List<KuaipanFile> childrens = file.getChildren();
	        if (childrens != null) {
	            int size = childrens.size();
	            for (int i = 0; i < size; i++) {
	                KuaipanFile children = childrens.get(i);
	                if (children.isDirectory()) {
	                    getAllFiles(list, childrens.get(i));
	                } else {
	                    list.add(children);
	                }
	            }
	        }
	    }
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		
	}
}
